<?php
class Controller {
	/**
	 * 视图对象
	 * @var object 
	 */
	protected $_view;
	
	/**
	 * 请求对象
	 *
	 * @var object
	 */
	protected $_request;
	
	/**
	 * 输出对象
	 *
	 * @var object
	 */
	protected $_response;
	
	/**
	 * 来源地址
	 * @var string 
	 */
	protected $_refer;

	final public function __construct() {
		$this->_response = new Response_Http();
		$this->_request = Dispatcher::getInstance()->getRequest();
		$this->_view = Dispatcher::getInstance()->getView();
		
		if ($this->getServer('HTTP_REFERER') == '' || strpos($this->getServer('HTTP_REFERER'), $this->getServer('HTTP_HOST')) === FALSE){
			$this->_refer = '';
		} else {
			$this->_refer = $this->getServer('HTTP_REFERER');
			if (strpos($this->_refer, '#') !== false) {
				$this->_refer = substr($this->_refer, 0, strpos($this->_refer,'#'));
			}
		}
		
		$this->init();
	}
	
	protected function init() {}
	
	/**
	 * 获取服务器环境变量$_SERVER
	 * 
	 * @param string $name 名称
	 */
	protected function getServer($name) {
		return $this->_request->getServer($name);
	}
	
	/*
	 * 检查主题的配置文件theme.ini
	 * @param string $theme 主题名称
	 */
	protected function checkThemeIni($theme, $module = '') {
		$iniFile = THEME_PATH.$theme.'/theme.ini';
		if (!is_file($iniFile)) {
			throw new CoreException(__("主题%s的配置文件theme.ini不存在", $theme));
		}
		$themeInfo = parse_ini_file($iniFile, false);
		if (empty($themeInfo) || empty($themeInfo['module'])) {
			throw new CoreException(__("theme.ini 缺少配置项: %s", 'module'));
		}
		if ($module && $themeInfo['module'] != $module) {
			throw new CoreException(__("主题%s与模块%s不匹配", $theme, $module));
		}
	}
	
	/**
	 * 设置页面标题
	 * @param mixed $title
	 */
	public function setTitle($title) {
		$this->view->setTitle($title);
	}
	
	/**
	 * 设置模板变量
	 *
	 * @param mixed $name 要显示的模板变量
	 * @param mixed $value 变量的值
	 */
	protected function assign($name, $value = '') {
		$this->view->assign($name, $value);
	}
	
	/**
	 * 渲染模版
	 * 
	 * @param string $filename 模版名，默认为：控制器名+动作名
	 * @param string $charset 字符编码
	 * @param string $contentType 内容类型
	 */
	protected function display($filename = '', $charset = self::CHARSET, $contentType = 'text/html') {
		if (empty($filename)) $filename = strtolower(CONTROL_NAME).'_'.strtolower(ACTION_NAME);
		$this->view->display($this->view->getFilePath($filename), $charset, $contentType);
	}
	
	/**
	 * 提示消息
	 * 
	 * @param string $message 提示消息
	 * @param string $redirect 跳转地址
	 */
	protected function message($message, $redirect = '') {
		$this->assign('message', $message);
		$this->assign('redirect', $redirect);
		$this->display('message');
	}	
	
	/**
	 * AJAX输出
	 * 
	 * @param mixed $data 返回数据
	 * @param int $status 状态号(0 成功)
	 * @param string $msg 返回消息
	 */
	protected function ajaxOut($data, $status = 0, $msg = '') {
		$out = array();
		$out['ret'] = $status;
		$out['msg'] = $msg;
		$out['data'] = $data;
		$this->_response->addBody(json_encode($out));
	}
}